# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import os
import re

# #################### 用户配置区域 ####################
WEBPAGE_CONFIG = {
    'output_dir': 'saved_pages',    # 网页保存目录
    'filename': None,               # 自动根据标题生成文件名，或手动指定如'article.html'
    'beautify_html': True,          # 是否美化HTML结构
    'custom_css': '''
        body { max-width: 1000px; margin: 0 auto; padding: 20px; }
        img { max-width: 100%; }
    '''
}

def save_webpage_html(url):
    """保存完整网页内容"""
    # 创建保存目录
    if not os.path.exists(WEBPAGE_CONFIG['output_dir']):
        os.makedirs(WEBPAGE_CONFIG['output_dir'])
    
    # 获取网页内容
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    response.encoding = 'utf-8'
    
    # 使用BeautifulSoup解析
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 确保基础HTML结构完整
    if not soup.html:
        soup.html = soup.new_tag('html')
    if not soup.head:
        soup.head = soup.new_tag('head')
        soup.html.insert(0, soup.head)
    if not soup.body:
        soup.body = soup.new_tag('body')
        soup.html.append(soup.body)
    
    # 添加自定义样式
    style_tag = soup.new_tag('style')
    style_tag.string = WEBPAGE_CONFIG['custom_css']
    soup.head.append(style_tag)
    
    # 生成文件名
    if not WEBPAGE_CONFIG['filename']:
        title = soup.title.string.strip() if soup.title else 'untitled'
        filename = re.sub(r'[\\/*?:"<>|]', '_', title)[:50] + '.html'
    else:
        filename = WEBPAGE_CONFIG['filename']
    
    # 美化HTML结构
    if WEBPAGE_CONFIG['beautify_html']:
        html_content = soup.prettify()
    else:
        html_content = str(soup)
    
    # 保存文件
    save_path = os.path.join(WEBPAGE_CONFIG['output_dir'], filename)
    with open(save_path, 'w', encoding='utf-8') as f:
        f.write(f'<!-- Saved from {url} -->\n')
        f.write(html_content)
    
    print(f"网页已保存至: {os.path.abspath(save_path)}")
    return save_path

if __name__ == "__main__":
    target_url = "https://blog.lcayun.com/2752.html"
    save_webpage_html(target_url)